package com.commodity.Dao.orderDaoImp;

import com.commodity.Dao.OrderDao;
import com.commodity.common.Global;
import com.commodity.entity.Order;
import com.commodity.entity.OrderSearchBean;
import com.commodity.utils.PaginateInfo;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class OrderDaoImp implements OrderDao {
    private final JdbcTemplate jdbcTemplate = new JdbcTemplate(Global.getDataSource());
    private final RowMapper<Order> MAPPER = new BeanPropertyRowMapper<>(Order.class);
    @Override
    public List<Order> findAll(OrderSearchBean osb, PaginateInfo pi) {
        String countSql = "select count(0) from t_order ";
        StringBuilder sb = new StringBuilder();
        List<Object> args = new ArrayList<>();
        if (StringUtils.hasText(osb.getOrderId())){
            sb.append(" and order_id = ?");
            args.add(osb.getOrderId());
        }
        if (StringUtils.hasText(osb.getCustomerId())){
            sb.append(" and customer_id = ?");
            args.add(osb.getCustomerId());
        }
        if (StringUtils.hasText(osb.getCommodityId())) {
            sb.append(" and commodity_id = ?");
            args.add(osb.getCommodityId());
        }
        if (osb.getQuantityFrom()!=null){
            sb.append(" and quantity >= ?");
            args.add(osb.getQuantityFrom());
        }
        if (osb.getQuantityTo()!=null){
            sb.append(" and quantity < ?");
            args.add(osb.getQuantityTo());
        }
        if (StringUtils.hasText(osb.getCustomerPhone())){
            sb.append(" and customer_phone like ?");
            args.add("%"+osb.getCustomerPhone()+"%");
        }
        if (osb.getOrderTimeFrom()!=null){
            sb.append(" and order_time >= ?");
            args.add(osb.getOrderTimeFrom());
        }
        if (osb.getOrderTimeTo()!=null){
            sb.append(" and order_time < ?");
            args.add(osb.getOrderTimeTo());
        }

        countSql += " where 1=1 "+sb.toString();
        int total = jdbcTemplate.queryForObject(countSql,Integer.class,args.toArray());
        pi.setTotal(total);

        String sql = "select id,order_id,customer_id,commodity_id,quantity,customer_phone,order_time,description from t_order";
        sql += " where 1=1 "+sb.toString() +" limit ?,?";
        args.add(pi.getOffset());
        args.add(pi.getLimit());

        return jdbcTemplate.query(sql,MAPPER,args.toArray());
    }

    @Override
    public int add(Order order) {
        String sql = "insert into t_order(order_id,customer_id,commodity_id,quantity,customer_phone,order_time,description) values(?,?,?,?,?,?,?)";
        return jdbcTemplate.update(sql,order.getOrderId(),order.getCustomerId(),order.getCommodityId(),order.getQuantity(),order.getCustomerPhone(),
        order.getOrderTime(),order.getDescription());
    }

    @Override
    public int deleteByIds(Integer[] intIds) {
        String sql = "delete from t_order where id in (";
        for (int i = 0; i < intIds.length; i++) {
            if (i==intIds.length-1){
                sql+="?";
            }else {
                sql+="?,";
            }
        }
        sql+=")";

        return jdbcTemplate.update(sql,intIds);
    }

    @Override
    public Order findById(Integer id) {
        String sql = "select * from t_order where id = ?";
        List<Order> orderList = jdbcTemplate.query(sql,MAPPER,id);
        return orderList.isEmpty()?null:orderList.get(0);
    }

    @Override
    public int update(Order order) {
        String sql = "update t_order set order_id = ?,customer_id = ?,commodity_id = ?,quantity = ?,customer_phone = ?,order_time = ?,description = ?" +
                "where id = ?";

        return jdbcTemplate.update(sql,order.getOrderId(),order.getCustomerId(),order.getCommodityId(),order.getQuantity(),order.getCustomerPhone(),
        order.getOrderTime(),order.getDescription(),order.getId());
    }

    @Override
    public List<Order> findByCommodityId(String commodityIdFromCommodity, PaginateInfo pi) {
        String countSql = "select count(*) from t_order where commodity_id = ?";
        int total = jdbcTemplate.queryForObject(countSql,Integer.class,commodityIdFromCommodity);
        pi.setTotal(total);
        String sql = "select * from t_order where commodity_id = ? limit ?,?";
        List<Order>  orderList = jdbcTemplate.query(sql,MAPPER,commodityIdFromCommodity,pi.getOffset(),pi.getLimit());
        return orderList.isEmpty()?null:orderList;
    }
}
